package cn.le.util;


import cn.le.exception.BizException;

import java.util.HashMap;
import java.util.Map;

/**
 * 项目名  jdrx-bigdata-metadata
 * Created by zhongdev.
 * Created at 2018/1/26
 * 描述:MapUitls
 */
public class MapUtils {

    public static Object getObjectPropertyFromMap(Map<String,?> properties, String property) throws BizException {
        Object propertyObject = properties.get(UnderLineToCamel.camelToUnderline(property));
        if(propertyObject==null){
            throw new BizException("缺少 "+property+" 属性值");
        }else{
            return propertyObject;
        }
    }
    public static String getPropertyFromMap(Map<String,?> properties, String property, boolean required) throws BizException {
        Object propertyObject = properties.get(UnderLineToCamel.camelToUnderline(property));
        if(propertyObject==null){
            if(required){
                throw new BizException("缺少 "+property+" 属性值");
            }else{
                return null;
            }
        }else{
            return String.valueOf(propertyObject);
        }
    }

    public static String getPropertyFromMap(Map<String,?> properties, String property) throws BizException {
        return getPropertyFromMap(properties, UnderLineToCamel.camelToUnderline(property),true);
    }

    public static void setPropertyFromMap(Map<String,String> properties,String property,String value) throws BizException {
        properties.put(UnderLineToCamel.camelToUnderline(property),value);
    }

    public static void setPropertyFromMap(Map properties,String property,Object value) throws BizException {
        properties.put(UnderLineToCamel.camelToUnderline(property),value);
    }

    public static boolean compareTo(Map<String,?> source, Map<String,?> target, String... ignoreProperties){
        Map<String,Object> ignorePropertyMap= new HashMap<>();
        for (String ignoreProperty : ignoreProperties) {
            ignorePropertyMap.put(ignoreProperty, null);
        }
        for (Map.Entry<String,?> entry:source.entrySet()) {
            String key = entry.getKey();
            if(ignorePropertyMap.containsKey(key)){
                continue;
            }
            Object value = entry.getValue();
            Object targetObject = target.get(key);
            if(targetObject==null&&value!=null){
                return false;
            }
            assert targetObject != null;
            if(targetObject.equals(value)){
            }else{
                return false;
            }
        }
        return true;
    }
}
